package com.itheima.info.manger.system.dao;

import com.itheima.info.manger.system.domain.Student;

/*
学生操作数据类

增
删
改
查询所有
根据学号查询
 */
public class StudentDao implements BaseStudentDao {
    private static Student[] students = new Student[40];

    static {
        Student student1 = new Student("0001", "蔡徐坤", 27, "1994");
        Student student2 = new Student("0002", "黄子韬", 26, "1995");
        Student student3 = new Student("0003", "鹿晗", 28, "1993");
        Student student4 = new Student("0004", "吴亦凡", 30, "1991");
        students[0] = student1;
        students[1] = student2;
        students[2] = student3;
        students[3] = student4;
    }
    public boolean addStudent(Student student) {
        //找个空位
        for (int i = 0; i < students.length; i++) {
            //students[i]
            Student stu = students[i];
            //判断stu 是否为空 为空放进来
            if (stu == null) {
                students[i] = student;
                return true;
            }
        }
        return false;
    }

    public void deleteStudent(String sid) {
        int index = getIndex(sid);
        students[index] = null;
    }

    public void updateStudent(String sid, Student newStu) {
        int index = getIndex(sid);
        students[index] = newStu;
    }

    public Student[] findAllStudents() {
        return students;
    }

    //查索引的方法
    //参数 学号
    //返回值 int
    public int getIndex(String sid) {
        for (int i = 0; i < students.length; i++) {
            //students[i]
            Student stu = students[i];
            if (stu != null && sid.equals(stu.getSid())) {
                return i;
            }
        }
        return -1;
    }
}
